package controllers;

import java.io.IOException;
import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import dao.EmployeeDAORemote;
import dto.EmployeeDTO;

@ManagedBean
@SessionScoped
public class NewPasswordController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	EmployeeDAORemote employeeDAO;

	@ManagedProperty(value = "#{employeeDTO}")
	EmployeeDTO oldEmployeeDTO, newEmployeeDTO;

	@ManagedProperty(value = "#{authentificationController}")
	private AuthentificationController authentificationController;

	private String pass1;
	private String pass2;
	private String messageClass = "notice";

	
	//update employees password
	public void updateEmployee(ActionEvent event) {
		oldEmployeeDTO = authentificationController.getEmployeeDTO();

		newEmployeeDTO = oldEmployeeDTO;

		FacesContext context = FacesContext.getCurrentInstance();
		if (!(pass1.equals(pass2))) {
			context.addMessage(null, new FacesMessage("The password update failed.Please enter two equal passwords"));
		} else if ((pass1.equals(pass2) && (pass1.equals("1234")) && (pass2.equals("1234")))) {
			context.addMessage(null, new FacesMessage(
					"Please enter another password.The default password is not allowed."));
		} else {
			if ((pass1.equals(pass2))) {
				newEmployeeDTO.setPassword(pass1);
				employeeDAO.updateEmployee(oldEmployeeDTO, newEmployeeDTO);
				try {
					FacesContext.getCurrentInstance().getExternalContext().redirect(
							"/MSGCandidateTesting/faces/employeeAuthentification.xhtml");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	///getters and setters
	
	public void setPass1(String pass1) {
		this.pass1 = pass1;
	}

	public String getPass1() {
		return pass1;
	}

	public void setPass2(String pass2) {
		this.pass2 = pass2;
	}

	public String getPass2() {
		return pass2;
	}

	public void setAuthentificationController(AuthentificationController authentificationController) {
		this.authentificationController = authentificationController;
	}

	public AuthentificationController getAuthentificationController() {
		return authentificationController;
	}

	/**
	 * @param messageClass
	 *            the messageClass to set
	 */
	public void setMessageClass(String messageClass) {
		this.messageClass = messageClass;
	}

	/**
	 * @return the messageClass
	 */
	public String getMessageClass() {
		return messageClass;
	}

	public EmployeeDTO getEmployeeDTO() {
		return oldEmployeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.oldEmployeeDTO = employeeDTO;
	}

}
